package com.resataurant.Dao;

import com.resataurant.Entity.Order;
import com.resataurant.Util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class OrderDao {

    public int addOrder(Order order) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            String sql = "insert into s_shop_log (w_id, w_name, w_contenct, w_price, w_user_name,w_user_id,w_data,w_num) values (?, ?, ?, ?, ?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, order.getW_id());
            preparedStatement.setString(2, order.getW_Name());
            preparedStatement.setString(3, order.getW_contenct());
            preparedStatement.setString(4, order.getW_Price());
            preparedStatement.setString(5, null);
            preparedStatement.setString(6, order.getW_user_id());
            preparedStatement.setString(7, order.getW_date());
            preparedStatement.setInt(8, 1);
            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(connection, preparedStatement, resultSet);
        }
        return 0;
    }

    public int addCartOrder(List<Order> orders) {

        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try{
            String sql = "insert into s_shop_log (w_id, w_name, w_contenct, w_price, w_user_name,w_user_id,w_data,w_num) values (?, ?, ?, ?, ?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            for (Order order : orders) {
                preparedStatement.setString(1, order.getW_id());
                preparedStatement.setString(2, order.getW_Name());
                preparedStatement.setString(3, null);
                preparedStatement.setString(4, order.getW_Price());
                preparedStatement.setString(5, null);
                preparedStatement.setString(6, order.getW_user_id());
                preparedStatement.setString(7, order.getW_date());
                preparedStatement.setInt(8, order.getW_num());
                preparedStatement.addBatch();
            }
            return preparedStatement.executeBatch().length;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }finally {
            JDBCUtils.close(connection, preparedStatement, resultSet);
        }
    }
}
